package com.cooper.resource.download;

import java.io.File;
import java.io.Serializable;
import java.util.Collection;

import com.cooper.android.comm.FileUtils;
import com.cooper.scraper.R;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.widget.Toast;

public abstract class DownloadingFragment extends ListFragment implements
		DownloadControl {

	protected static final String ADD_DOWN_PARAM_NAME = "ADD_DOWN_PARAM_NAME";

	protected abstract void onCompleteDown(DownloadStatus res);

	protected abstract void onItemCountChange(int count);

	protected abstract void onItemCancel(DownloadStatus res);

	protected abstract void onCancelAll();

	protected boolean completeDown(DownloadStatus res) {
		if (((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.completeMove(res)) {
			adapter.removeItem(res);
			onItemCountChange(adapter.getCount());
			return true;
		}
		return false;
	}

	protected void doItemCancel(DownloadStatus res) {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder().cancel(
				res);
		adapter.removeItem(res);
		onItemCountChange(adapter.getCount());
	}

	protected void doCancelAllItem() {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.cancelAll();
		adapter.removeAll();
		onItemCountChange(adapter.getCount());
	}

	@Override
	public void pause(DownloadStatus res) {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder().pause(
				res);
	}

	@Override
	public void pauseAll() {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.pauseAll();
	}

	@Override
	public void continueAll() {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.continueAll();
	}

	@Override
	public void cancelAll() {
		onCancelAll();
	}

	@Override
	public void downContinue(DownloadStatus res) {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.downContinue(res);
	}

	@Override
	public void cancel(DownloadStatus res) {
		onItemCancel(res);
	}

	@Override
	public boolean completeMove(DownloadStatus res) {
		if (res.getDownloadResource().isFinish()) {
			onCompleteDown(res);
		}
		return false;
	}

	public Collection<DownloadStatus> getDownloading() {
		return ((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.getDownloading();
	}

	private DownloadItemAdapter adapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		adapter = new DownloadItemAdapter(getActivity(),
				DownloadingFragment.this);
		setListAdapter(adapter);
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.setDownloadListener(adapter);
		
		/*
		if (getArguments() != null) {
			Serializable downData = getArguments().getSerializable(
					ADD_DOWN_PARAM_NAME);
			if (downData != null) {

				DownloadResource requestRes = (DownloadResource) downData;
				getArguments().remove(ADD_DOWN_PARAM_NAME);

				addDownload(requestRes);

			}
		}*/
	}
	
	/*

	private void addDownload(DownloadResource requestRes) {

		Log.d("DownloadingActivity", requestRes.getDownloadTitle());
		File storeDir = requestRes.getDownloadingSaveDir();
		if (storeDir.exists()) {
			Log.d("DownloadingActivity", "Down is exists");
			if (!((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
					.downloadPrefIsExists(storeDir)) {
				FileUtils.delete(storeDir);
			} else {
				Toast.makeText(getActivity(), R.string.download_task_exists,
						Toast.LENGTH_LONG).show();
				return;
			}
		}
		adapter.addDownload(((DownloadServiceMgr) getActivity())
				.getDownloadServerBinder().addDownload(requestRes));
		onItemCountChange(adapter.getCount());

	}
*/
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
